Install and load the tmap and spDataLarge packages (available from GitHub). Load and review the pol_pres15 dataset on the Polish Presidential election in 2015

# install required packages
install.packages("tmap", repos = "https://r-tmap.github.io/tmap/", type = "source")
## Warning: unable to access index for repository https://r-tmap.github.io/tmap/src/contrib:
##   cannot open URL 'https://r-tmap.github.io/tmap/src/contrib/PACKAGES'
## Warning: package 'tmap' is not available for this version of R
## 
## A version of this package for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/", type = "source")

library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(spDataLarge)

# load the tmap and spDataLarge packages
pacman::p_load(
  ggplot2,
  spatstat,
  spatialreg,
  spgwr,
  spdep,
  adehabitatHR,
  googleway,
  gmapsdistance,
  leaflet,
  dismo,
  raster,
  sp,
  leaflet,
  tmap,
  foreign,
  sf,
  terra,
  RColorBrewer,
  stargazer,
  rmapshaper
)

# load the dataset
data("pol_pres15")

summary(pol_pres15)
##     TERYT              TERYT0             name0               name          
##  Length:2495        Length:2495        Length:2495        Length:2495       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##      gm0                       types      I_entitled_to_vote
##  Length:2495        Rural         :1563   Min.   :  1308    
##  Class :character   Urban         : 303   1st Qu.:  4026    
##  Mode  :character   Urban/rural   : 611   Median :  6033    
##                     Warsaw Borough:  18   Mean   : 12221    
##                                           3rd Qu.: 10524    
##                                           Max.   :594643    
##  I_voting_papers_received I_unused_voting_papers
##  Min.   :  1000           Min.   :   204        
##  1st Qu.:  3500           1st Qu.:  1708        
##  Median :  5200           Median :  2545        
##  Mean   : 10530           Mean   :  4577        
##  3rd Qu.:  9104           3rd Qu.:  4112        
##  Max.   :505747           Max.   :200431        
##  I_voting_papers_issued_to_voters I_voters_voting_by_proxy
##  Min.   :   561                   Min.   :  0.000         
##  1st Qu.:  1698                   1st Qu.:  0.000         
##  Median :  2654                   Median :  1.000         
##  Mean   :  5954                   Mean   :  2.387         
##  3rd Qu.:  4988                   3rd Qu.:  3.000         
##  Max.   :338252                   Max.   :119.000         
##  I_voters_voting_by_declaration I_voters_sent_postal_voting_package
##  Min.   :   0.00                Min.   :  0.000                    
##  1st Qu.:   7.00                1st Qu.:  0.000                    
##  Median :  15.00                Median :  0.000                    
##  Mean   :  49.98                Mean   :  3.524                    
##  3rd Qu.:  31.00                3rd Qu.:  2.000                    
##  Max.   :7583.00                Max.   :577.000                    
##  I_postal_voting_envelopes_received I_PVE_of_which_no_declaration
##  Min.   :  0.000                    Min.   : 0.0000              
##  1st Qu.:  0.000                    1st Qu.: 0.0000              
##  Median :  0.000                    Median : 0.0000              
##  Mean   :  3.066                    Mean   : 0.1455              
##  3rd Qu.:  2.000                    3rd Qu.: 0.0000              
##  Max.   :281.000                    Max.   :34.0000              
##  I_PVE_of_which_no_signature I_PVE_of_which_no_voting_envelope
##  Min.   :0.00000             Min.   :0.000000                 
##  1st Qu.:0.00000             1st Qu.:0.000000                 
##  Median :0.00000             Median :0.000000                 
##  Mean   :0.00481             Mean   :0.004008                 
##  3rd Qu.:0.00000             3rd Qu.:0.000000                 
##  Max.   :4.00000             Max.   :2.000000                 
##  I_PVE_of_which_voting_envelope_open I_voting_envelopes_placed_in_ballot_box
##  Min.   :0.000000                    Min.   :  0.000                        
##  1st Qu.:0.000000                    1st Qu.:  0.000                        
##  Median :0.000000                    Median :  0.000                        
##  Mean   :0.007615                    Mean   :  2.897                        
##  3rd Qu.:0.000000                    3rd Qu.:  2.000                        
##  Max.   :2.000000                    Max.   :272.000                        
##  I_voting_papers_taken_from_ballot_box
##  Min.   :   561                       
##  1st Qu.:  1698                       
##  Median :  2656                       
##  Mean   :  5956                       
##  3rd Qu.:  4990                       
##  Max.   :338444                       
##  I_of_which_voting_papers_taken_from_voting_envelopes I_invalid_voting_papers
##  Min.   :  0.000                                      Min.   :  0.0000       
##  1st Qu.:  0.000                                      1st Qu.:  0.0000       
##  Median :  0.000                                      Median :  0.0000       
##  Mean   :  2.906                                      Mean   :  0.5623       
##  3rd Qu.:  2.000                                      3rd Qu.:  0.0000       
##  Max.   :272.000                                      Max.   :134.0000       
##  I_valid_voting_papers I_invalid_votes   I_valid_votes    I_candidates_total
##  Min.   :   561        Min.   :   0.00   Min.   :   550   Min.   :   550    
##  1st Qu.:  1698        1st Qu.:  13.00   1st Qu.:  1682   1st Qu.:  1682    
##  Median :  2656        Median :  22.00   Median :  2631   Median :  2631    
##  Mean   :  5955        Mean   :  49.57   Mean   :  5906   Mean   :  5906    
##  3rd Qu.:  4990        3rd Qu.:  41.00   3rd Qu.:  4946   3rd Qu.:  4946    
##  Max.   :338410        Max.   :2785.00   Max.   :335625   Max.   :335625    
##  I_Grzegorz.Michal.Braun I_Andrzej.Sebastian.Duda I_Adam.Sebastian.Jarubas
##  Min.   :   0.00         Min.   :    44           Min.   :   3.00         
##  1st Qu.:  10.00         1st Qu.:   665           1st Qu.:  33.00         
##  Median :  18.00         Median :  1094           Median :  55.00         
##  Mean   :  47.82         Mean   :  2060           Mean   :  95.49         
##  3rd Qu.:  38.00         3rd Qu.:  1956           3rd Qu.:  89.00         
##  Max.   :4010.00         Max.   :107367           Max.   :7154.00         
##  I_Bronislaw.Maria.Komorowski I_Janusz.Ryszard.Korwin.Mikke
##  Min.   :    55.0             Min.   :    7.0              
##  1st Qu.:   412.5             1st Qu.:   45.0              
##  Median :   727.0             Median :   76.0              
##  Mean   :  2002.5             Mean   :  189.6              
##  3rd Qu.:  1455.5             3rd Qu.:  153.0              
##  Max.   :127361.0             Max.   :15505.0              
##  I_Marian.Janusz.Kowalski I_Pawel.Piotr.Kukiz I_Magdalena.Agnieszka.Ogorek
##  Min.   :   0.00          Min.   :   69       Min.   :   7                
##  1st Qu.:   8.00          1st Qu.:  305       1st Qu.:  40                
##  Median :  15.00          Median :  498       Median :  62                
##  Mean   :  30.53          Mean   : 1217       Mean   : 141                
##  3rd Qu.:  27.00          3rd Qu.: 1008       3rd Qu.: 114                
##  Max.   :1966.00          Max.   :65702       Max.   :7605                
##  I_Janusz.Marian.Palikot I_Pawel.Jan.Tanajno  I_Jacek.Wilk    
##  Min.   :   0.00         Min.   :   0.00     Min.   :   0.00  
##  1st Qu.:  15.00         1st Qu.:   1.00     1st Qu.:   5.00  
##  Median :  26.00         Median :   3.00     Median :  10.00  
##  Mean   :  83.58         Mean   :  11.69     Mean   :  27.05  
##  3rd Qu.:  53.00         3rd Qu.:   7.00     3rd Qu.:  20.00  
##  Max.   :5942.00         Max.   :1278.00     Max.   :2166.00  
##  II_entitled_to_vote II_voting_papers_received II_unused_voting_papers
##  Min.   :  1302      Min.   :  1099            Min.   :    50         
##  1st Qu.:  4042      1st Qu.:  3500            1st Qu.:  1419         
##  Median :  6030      Median :  5200            Median :  2115         
##  Mean   : 12205      Mean   : 10543            Mean   :  3801         
##  3rd Qu.: 10510      3rd Qu.:  9106            3rd Qu.:  3431         
##  Max.   :596651      Max.   :505401            Max.   :162901         
##  II_voting_papers_issued_to_voters II_voters_voting_by_proxy
##  Min.   :   630                    Min.   :  0.0            
##  1st Qu.:  1992                    1st Qu.:  0.0            
##  Median :  3080                    Median :  2.0            
##  Mean   :  6743                    Mean   :  3.6            
##  3rd Qu.:  5667                    3rd Qu.:  4.0            
##  Max.   :378687                    Max.   :173.0            
##  II_voters_voting_by_declaration II_voters_sent_postal_voting_package
##  Min.   :    0.0                 Min.   :  0.000                     
##  1st Qu.:   18.0                 1st Qu.:  0.000                     
##  Median :   32.0                 Median :  0.000                     
##  Mean   :  100.5                 Mean   :  5.049                     
##  3rd Qu.:   68.0                 3rd Qu.:  3.000                     
##  Max.   :11427.0                 Max.   :537.000                     
##  II_postal_voting_envelopes_received II_PVE_of_which_no_declaration
##  Min.   :  0.000                     Min.   : 0.0000               
##  1st Qu.:  0.000                     1st Qu.: 0.0000               
##  Median :  0.000                     Median : 0.0000               
##  Mean   :  4.589                     Mean   : 0.1611               
##  3rd Qu.:  2.000                     3rd Qu.: 0.0000               
##  Max.   :489.000                     Max.   :46.0000               
##  II_PVE_of_which_no_signature II_PVE_of_which_no_voting_envelope
##  Min.   :0.00000              Min.   :0.000000                  
##  1st Qu.:0.00000              1st Qu.:0.000000                  
##  Median :0.00000              Median :0.000000                  
##  Mean   :0.01764              Mean   :0.008016                  
##  3rd Qu.:0.00000              3rd Qu.:0.000000                  
##  Max.   :8.00000              Max.   :2.000000                  
##  II_PVE_of_which_voting_envelope_open II_voting_envelopes_placed_in_ballot_box
##  Min.   :0.000000                     Min.   :  0.000                         
##  1st Qu.:0.000000                     1st Qu.:  0.000                         
##  Median :0.000000                     Median :  0.000                         
##  Mean   :0.008818                     Mean   :  4.386                         
##  3rd Qu.:0.000000                     3rd Qu.:  2.000                         
##  Max.   :6.000000                     Max.   :468.000                         
##  II_voting_papers_taken_from_ballot_box
##  Min.   :   630                        
##  1st Qu.:  1994                        
##  Median :  3081                        
##  Mean   :  6747                        
##  3rd Qu.:  5670                        
##  Max.   :379097                        
##  II_of_which_voting_papers_taken_from_voting_envelopes II_invalid_voting_papers
##  Min.   :  0.000                                       Min.   : 0.0000         
##  1st Qu.:  0.000                                       1st Qu.: 0.0000         
##  Median :  0.000                                       Median : 0.0000         
##  Mean   :  4.385                                       Mean   : 0.3872         
##  3rd Qu.:  2.000                                       3rd Qu.: 0.0000         
##  Max.   :467.000                                       Max.   :66.0000         
##  II_valid_voting_papers II_invalid_votes  II_valid_votes  
##  Min.   :   630         Min.   :   4.00   Min.   :   616  
##  1st Qu.:  1994         1st Qu.:  25.00   1st Qu.:  1967  
##  Median :  3081         Median :  41.00   Median :  3041  
##  Mean   :  6746         Mean   :  99.39   Mean   :  6647  
##  3rd Qu.:  5670         3rd Qu.:  78.00   3rd Qu.:  5592  
##  Max.   :379059         Max.   :5823.00   Max.   :373236  
##  II_Andrzej.Sebastian.Duda II_Bronislaw.Maria.Komorowski          geometry   
##  Min.   :   133            Min.   :    80.0              MULTIPOLYGON :2495  
##  1st Qu.:  1142            1st Qu.:   676.5              epsg:2180    :   0  
##  Median :  1820            Median :  1174.0              +proj=tmer...:   0  
##  Mean   :  3424            Mean   :  3223.4                                  
##  3rd Qu.:  3200            3rd Qu.:  2378.5                                  
##  Max.   :176990            Max.   :201473.0                                  
##    I_turnout        II_turnout      I_Duda_share     II_Duda_share   
##  Min.   :0.2634   Min.   :0.3363   Min.   :0.06433   Min.   :0.1540  
##  1st Qu.:0.3989   1st Qu.:0.4661   1st Qu.:0.30362   1st Qu.:0.4520  
##  Median :0.4448   Median :0.5102   Median :0.38993   Median :0.5775  
##  Mean   :0.4455   Mean   :0.5112   Mean   :0.40091   Mean   :0.5809  
##  3rd Qu.:0.4891   3rd Qu.:0.5559   3rd Qu.:0.48993   3rd Qu.:0.7091  
##  Max.   :0.6768   Max.   :0.7548   Max.   :0.78566   Max.   :0.9508  
##  I_Komorowski_share II_Komorowski_share
##  Min.   :0.03747    Min.   :0.04917    
##  1st Qu.:0.20285    1st Qu.:0.29087    
##  Median :0.29594    Median :0.42254    
##  Mean   :0.29540    Mean   :0.41915    
##  3rd Qu.:0.38709    3rd Qu.:0.54796    
##  Max.   :0.66901    Max.   :0.84602

One visualization should compare the support for Komorowski and Duda

# setting color palette
display.brewer.all()

# first round vote share of komorowski and duda on choropleth
tm_shape(pol_pres15) + 
  tm_fill("I_Duda_share", palette = "RdYlGn", style = "equal", n = 2, breaks = c(0, 0.49, 1), 
          labels = c("Komoroski", "Duda")) + 
  tm_borders(alpha = 0.5, col = "white") + 
  tm_compass() + 
  tm_layout(title = "First Round Vote Share of Komorowski and Duda", 
            legend.text.size = 1, legend.title.size = 0.01, 
            legend.position = c("left", "bottom"), legend.title.color = "white", frame = FALSE)

# runoff vote share of komorowski and duda on choropleth
tm_shape(pol_pres15) + 
  tm_fill("II_Duda_share", palette = "RdYlGn", style = "equal", n = 2, alpha = 1, breaks = c(0, 0.49, 1), 
          labels = c("Komoroski", "Duda")) + 
  tm_borders(alpha = 0.5, col = "white") + 
  tm_dots("types", palette = "BrBG") +
  tm_compass() + 
  tm_layout(title = "Runoff Round Vote Share of Komorowski and Duda", 
            legend.text.size = 1, legend.title.size = 0.01, 
            legend.position = c("left", "bottom"), legend.title.color = "white", frame = FALSE)

# percentage of vote share by each candidate
tm_shape(pol_pres15) + 
  tm_fill("II_Duda_share", palette = "RdYlGn", style = "equal", n = 10, breaks = c(0, 0.49, 1), 
          labels = c("Komoroski (91-100)","Komoroski (90-81)", "Komoroski (80-71)","Komoroski (70-61)","Komoroski (60-51)", "Duda (51-60)","Duda (61-70)","Duda (71-80)","Duda (81-90)","Duda (91-100)")) + 
  tm_borders(alpha = 0.5, col = "black") + 
  tm_compass() + 
  tm_layout(title = "Percentage of Vote Share by Each Candidate", 
            legend.text.size = 0.55, legend.title.size = 0.01, 
            legend.position = c("left", "bottom"), legend.title.color = "white", frame = FALSE)

One visualization should investigate possible issues with postal voting envelopes

# correlation of unused voting papers
plot(II_unused_voting_papers ~ II_of_which_voting_papers_taken_from_voting_envelopes, 
     data = pol_pres15, 
     xlab = "Voting papers taken from voting envelopes", 
     ylab = "Unused Voting Papers", 
     col = "blue")

plot(II_unused_voting_papers ~ II_voting_papers_taken_from_ballot_box, 
     data = pol_pres15, 
     xlab = "Voting papers taken from Ballot Box", 
     ylab = "Unused Voting Papers", 
     col = "red")

# correlation of invalid votes
plot(II_invalid_votes ~ II_of_which_voting_papers_taken_from_voting_envelopes, data = pol_pres15, 
     ylab = "Number of Invalid Votes", 
     xlab = "Voting papers taken from voting envelopes", 
     col = "blue")

plot(II_invalid_votes ~ II_voting_papers_taken_from_ballot_box, 
     data = pol_pres15, 
     ylab = "Number of Invalid Votes", 
     xlab = "Voting papers taken from Ballot Box", 
     col = "red")